# -*- coding: utf-8 -*
#!/usr/bin/python3
#
# 游戏显示类
# 将游戏中的数据打印到界面中
#


import sys
import os
sys.path.append(os.getcwd() + "\\2048")
print(os.getcwd() + "\\2048")
import tkinter as tk
from Core.Data import GameData
from Core.Controller import GameController
from Core.Controller.OperationController import Operation 


class Internal_Mumber:
    root = None
    title = None
    title_txt = None
    content = None
    content_txt = None
    end = None
    end_txt = None
    title_format_txt = "按上下左右移动   最高分数：{0}"

    __editor = None
    __editor_txt = None

    def __init__(self):
        self.__InitComponent()

    def __InitComponent(self):
        self.title_format_txt = "按上下左右移动   最高分数：{0}"

        self.root = tk.Tk()
        self.root.minsize(200, 300)
        self.root.maxsize(200, 300)

        self.title = tk.Label(self.root, width=30, height=3,anchor='n', justify='center')
        self.title_txt = tk.StringVar()
        self.title["textvariable"] = self.title_txt
        self.title_txt.set(self.title_format_txt.format(0))
        self.title.pack()

 
        self.content = tk.Label(self.root, width=50, height=6, anchor='n', justify='center')
        self.content_txt = tk.StringVar()
        self.content["textvariable"] = self.content_txt
        self.content_txt.set(self.title_format_txt.format(0))
        self.content.pack()

        self.end = tk.Label(self.root, width=30, height=3,anchor='s',  justify='center',)
        self.end_txt = tk.StringVar()
        self.end["textvariable"] = self.end_txt
        self.end_txt.set("")
        self.end.pack()

        self.__editor = tk.Label(self.root, width=30, height=3,anchor='s',  justify='center',)
        self.__editor_txt = tk.StringVar()
        self.__editor["textvariable"] = self.__editor_txt
        self.__editor_txt.set("作者: 杨庆城 \n http://blog.csdn.net/biospc")
        self.__editor.pack()





class DisplayController:

    __internal_mumber = None

    @staticmethod
    def InitFunc():
        GameController.GameController.SetDisplayFunc(DisplayController.Display,DisplayController.DisplayDeath)

        global __internal_mumber
        __internal_mumber = Internal_Mumber()
        __internal_mumber.root.bind("<Key>",lambda event_code:DisplayController.InputKey(event_code))
        DisplayController.Display()
        __internal_mumber.root.mainloop()

        

    @staticmethod
    def InputKey(event):
        global __internal_mumber 
        #__internal_mumber.content_txt.set("你按下了：  \nevent.keycode: " + str(event.keycode) + "\nevent.char: " + str(event.char))
        keycode = event.keycode
        print(keycode)

        opt_action = None
        if keycode == 38 or keycode == 87:
            opt_action = Operation.Up
        elif keycode == 40 or keycode == 83:
            opt_action = Operation.Down
        elif keycode == 37 or keycode == 65:
            opt_action = Operation.Left
        elif keycode == 39 or keycode == 68:
            opt_action = Operation.Right
        elif keycode == 67:
            if GameController.GameController.CheckGameIsDied():
                GameController.GameController.InitGame()
                global __internal_mumber
                __internal_mumber.end_txt.set("")
                opt_action = None
                GameController.GameController.SetDisplayFunc(DisplayController.Display,DisplayController.DisplayDeath)
                DisplayController.Display()
        else:
            opt_action = None
        
        
        if opt_action != None:
            GameController.GameController.GameOptAction(opt_action)
            
        

    @staticmethod
    def Display():
        global __internal_mumber
        __internal_mumber.title_txt.set(__internal_mumber.title_format_txt.format(GameData.GameData.GetResult()))
        content = DisplayController.__GetFormatContentTxt()
        __internal_mumber.content_txt.set(content)
    
    @staticmethod
    def DisplayDeath():
        global __internal_mumber
        __internal_mumber.end_txt.set("游戏结束 \n 按 c 键盘重新开始")

    @staticmethod
    def __GetFormatContentTxt():
        content = ""
        data = GameData.GameData.GetGameData()
        for row in iter(range(len(data))):
            if row >= 0:
                content = content + "\n"
            #content = content + "\t"
            data_item = data[row]
            for col in iter(range(len(data_item))):
                content =  content + "{:6d}".format(int(data_item[col]))


        print("__GetFormatContentTxt()\n" , content)
        return content



DisplayController.InitFunc()
# DisplayController.Display()
